package com.sfpay.coreplatform.order.service.composite.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.common.enums.tradeStatus;
import com.sfpay.coreplatform.order.service.composite.common.PayContext;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException;
import com.sfpay.coreplatform.order.valueobject.dto.InnerResponse;
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.dto.Response;
@Service
public class TradeStatusHandler extends AbstractHandler {

	private final static Logger logger = LoggerFactory
			.getLogger(TradeStatusHandler.class);
	@Override
	public Response Handler(PayRequest request, PayContext payContext)throws OrderServiceException {
		InnerResponse response = null;
		try {
			this.getTradeOrderDao().updateTradeState(payContext.getTradeNo(),
					payContext.getAccCode()>0?tradeStatus.SUCCESS:tradeStatus.FAILURE, null);
			response = new InnerResponse();
			response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);

		} catch (Exception e) {
			logger.error(
					"method:statusTradeHandle tradeNo:" + payContext.getTradeNo()
							+ " update status exception ", e);
			throw new OrderServiceException(
					OrderResponseCode.ORDER_DB_EXCEPTION,
					"method:statusTradeHandle tradeNo:" + payContext.getTradeNo()
							+ " update status exception ", e);

		}

		return response;
	}
	@Override
	public String getHandlerName() {
		 
		return "TradeStatusHandler";
	}

}
